<template>
  <div>
    <el-card>
      <template #header>
        <el-row>
          <el-button type="primary" @click="add">新增</el-button>
          <el-button type="primary" @click="getKey">获取工作密钥</el-button>
          <el-button type="primary" @click="registerInfo">省平台注册</el-button>
          <el-button type="primary" @click="updateInfo">修改注册信息</el-button>
          <el-button type="primary" @click="getTokenInfo"
            >手动获取tokon</el-button
          >
        </el-row>
      </template>
      <el-table
        ref="enterpriseRef"
        :data="tableData"
        style="width: 100%; max-height: 61vh; height: 61vh"
        table-layout="fixed"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
      >
        <el-table-column fixed type="selection" width="55" />
        <el-table-column fixed prop="cmpName" label="企业名称" show-overflow-tooltip/>
        <el-table-column prop="energyConsumeLevel" label="能耗级别" />
        <el-table-column prop="enterpriseCode" label="统一社会信用代码" width="180"/>
        <el-table-column prop="isorg" label="是否公共机构" />
        <el-table-column prop="corporationName" label="法人代表" />
        <el-table-column prop="registerPrincipal" label="注册资金(万元)" />
        <el-table-column prop="registerDate" label="注册日期" />
        <el-table-column prop="secretKey" label="工作密钥" show-overflow-tooltip/>
        <el-table-column prop="dataIndex" label="是否注册" show-overflow-tooltip/>
        <el-table-column fixed="right" label="操作" width="160">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="rowChange(scope.$index, scope.row)"
              ><el-icon class="el-input__icon"><Edit /></el-icon
            ></el-button>
            <el-button
              link
              type="danger"
              @click="deleteById(scope.$index, scope.row)"
              ><el-icon class="el-input__icon"><Delete /></el-icon
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="card-footer">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[10,20,30,40,50,100,500]"
          background
          v-model:page-size="pagination.size"
          v-model:current-page="pagination.page"
          :total="pagination.total"
          @size-change="sizeChange"
          @current-change="pageChange"
          class="pagination"
        />
      </div>
      <!-- 修改或者新增对话框 -->
      <el-dialog v-model="dialogFormVisible" :title="dialogTitle" style="min-width: 1450px;">
        <el-form :model="ruelForm" :inline="true" class="demo-form-inline" label-position="right" label-width="130" :rules="rules">
          <el-form-item label="企业名称">
            <el-input
              v-model="ruelForm.cmpName"
              placeholder="请输入企业名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="统一社会信用代码">
            <el-input
              v-model="ruelForm.enterpriseCode"
              placeholder="请输入社会信用代码"
              clearable
            />
          </el-form-item>
          <el-form-item label="是否公共机构">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.isorg"
              placeholder="请选择"
              clearable
            />
          </el-form-item>
          <el-form-item label="地址">
            <el-input
              v-model="ruelForm.address"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="是否央企">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.center"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="法人代表">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.corporationName"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="记录索引号">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.dataIndex"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="法人电子邮箱">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.email"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="企业能耗级别">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.energyConsumeLevel"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="传真">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.fax"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="领域编码">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.fieldCode"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="集团地址">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.center"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="集团名称">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.groupName"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="行业编码">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.industryCode"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="是否能源加工转换类企业">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.jgzh"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="纬度">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.latitude"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="企业主导产品">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.leadingProduct"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="营业执照电子照片">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.license"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="经度">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.longitude"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="机构类型">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.orgType"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="企业组织机构电子照片">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.organization"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="省级平台访问密码">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.organization"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="法人联系电话">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.phone"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="企业主要生产线名称">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.productionLine"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="行政区划代码">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.regionCode"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="注册地址">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.registerAddress"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="注册时间">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.registerDate"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="注册资金">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.registerPrincipal"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.remark"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="工作密钥">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.secretKey"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="单位类型">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.typeCode"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="用能单位网站地址">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.url"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
          <el-form-item label="邮编">
            <!-- SELECT -->
            <el-input
              v-model="ruelForm.zipCode"
              placeholder="请输入内容"
              clearable
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="dialogFormVisible = false">
              确定
            </el-button>
          </span>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import {
  enterNamePageList,
  getSecretKey,
  register,
  updateByIdRegister,
  getToken,
  logicDelete,
} from "@/api/enterprise";
import { ElMessage,ElMessageBox } from "element-plus";

const dialogFormVisible = ref(false);
const loading = ref(true);
const conditions = ref([
  {
    column: "distinct",
    type: "distinct",
    value: "true",
  },
]);
const dialogTitle = ref("");
const diaType = ref(true); //true为添加,false为更改
const selectId = ref([]);
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0,
});
const tableData = ref([]);
const ruelForm = reactive({
  name: "",
  region: "",
});
const rules = reactive({
  region: [
    {
      required: true,
      message: 'Please select Activity zone',
      trigger: 'change',
    },
  ],
})
const doDataList = () => {
  loading.value = true;
  let datas = {
    pageNum: pagination.page,
    pageSize: pagination.size,
    condition: JSON.stringify(conditions.value),
  };

  enterNamePageList(datas)
    .then((res) => {
      //获取到信息
      tableData.value = res.data.records;
      pagination.size = res.data.pageSize;
      pagination.page = res.data.currentPage;
      pagination.total = res.data.total;
      //请求完成,开始处理数据
      loading.value = false;
    })
    .catch((msg) => {
      //获取出错
      ElMessage.error("获取信息失败，请联系管理员！");
    });
};

const sizeChange = (value) => {
  pagination.size = value;
  pagination.page = 1;
  doDataList();
};
/**
 * 页总数改变
 */
const pageChange = (value) => {
  pagination.page = value;
  doDataList();
};
const handleSelectionChange = (value) => {
  selectId.value = value;
};
const listCheck = () => {
  if (selectId.value.length > 1) {
    ElMessage("选择数据不能大于一条！");
    return false;
  } else if (selectId.value.length === 0) {
    ElMessage("请选择至少一条数据！");
    return false;
  }
  return true;
};

const add = () => {
  dialogTitle.value = "添加";
  diaType.value = true;
  dialogFormVisible.value = true;
};
/**
 * 获取工作秘钥
 */
const getKey = () => {
  if (listCheck() != false) {
    let datas = {
      enterpriseId: selectId.value[0].enterpriseId,
    };
    getSecretKey(datas)
      .then((res) => {
        if (res.status == 200) {
          ElMessage.success("获取成功");
          doDataList();
        } else {
          ElMessage.error("获取失败," + res.message);
        }
      })
      .catch((msg) => {
        //获取出错
        ElMessage.error("获取工作密钥失败，请联系管理员！");
      });
  }
};
/**
 * 省平台注册
 */
const registerInfo = () => {
  if (listCheck() != false) {
    let datas = {
      enterpriseId: selectId.value[0].enterpriseId,
    };
    register(datas)
      .then((res) => {
        if (res.status == 200) {
          ElMessage.success("注册成功");
          doDataList();
        } else {
          ElMessage.error("注册失败," + res.message);
        }
      })
      .catch((msg) => {
        //获取出错
        ElMessage.error("注册失败，请联系管理员！");
      });
  }
};
/**
 * 修改注册信息
 */
const updateInfo = () => {
  if (listCheck() != false) {
    let datas = {
      enterpriseId: selectId.value[0].enterpriseId,
    };
    updateByIdRegister(datas)
      .then((res) => {
        if (res.status == 200) {
          ElMessage.success("修改成功");
          doDataList();
        } else {
          ElMessage.error("修改失败," + res.message);
        }
      })
      .catch((msg) => {
        //获取出错
        ElMessage.error("修改失败，请联系管理员！");
      });
  }
};
/**
 * 获取token
 */
const getTokenInfo = () => {
  if (listCheck() != false) {
    let datas = {
      enterpriseId: selectId.value[0].enterpriseId,
    };
    getToken(datas)
      .then((res) => {
        if (res.status == 200) {
          ElMessage.success("获取成功");
          doDataList();
        } else {
          ElMessage.error("获取失败," + res.message);
        }
      })
      .catch((msg) => {
        //获取出错
        ElMessage.error("获取失败，请联系管理员！");
      });
  }
};
const deleteById = (index, rowData) => {
  ElMessageBox.alert("确定删除该企业信息吗？", "删除操作", {
    confirmButtonText: "OK",
    callback: () => {
      let datas = {
        enterpriseId: rowData.enterpriseId,
      };
      logicDelete(datas)
        .then((res) => {
          if (res.status == 200) {
            ElMessage.success("删除成功");
            doDataList();
          } else {
            ElMessage.error("删除失败," + res.message);
          }
        })
        .catch((msg) => {
          //获取出错
          ElMessage.error("删除失败，请联系管理员！");
        });
    },
  });
};

doDataList();
</script>

<style lang="less" scoped>
.title {
  min-width: 5em;
}
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
.el-card__header {
  max-height: 10vh;
}
.pagination {
  max-height: 3vh;
}
.demo-form-inline .el-input {
  --el-input-width: 300px;
}
.el-form-item__label{
  line-height: 25px;
}
.card-footer {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
}
.total-font {
  margin-right: 15px;
  font-weight: 500;
  line-height: 21px;
  color: #595959;
}
</style>
